//
//  Cook_Thir_ViewController.m
//  Cook Street
//
//  Created by dllo on 15/8/8.
//  Copyright (c) 2015年 LANOU. All rights reserved.
//

#import "Cook_Thir_ViewController.h"
#import "AFN.h"
#define SCREEN_WIDTH self.view.bounds.size.width
#define SCREEN_HEIGHT self.view.bounds.size.height
#import "UIImageView+WebCache.h"
#import "Cook_Thir_View.h"
#import "Cook_Thir_TableViewCell.h"
#import "HeightForText.h"
#import "DataBase.h"
#import "CookModel.h"
//#import "UMSocial.h"


@interface Cook_Thir_ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (nonatomic, retain) UIImageView *img;  // 图片
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) Cook_Thir_View *cook_Thir_View;


@property (nonatomic, retain) UILabel *tagBgLabel;

@property (nonatomic, assign) CGFloat height;


//用料
@property (nonatomic, retain) UILabel *materials;
@property (nonatomic, retain) UILabel *mat_main;
@property (nonatomic, retain) UILabel *mat_other;

@property (nonatomic, retain) UILabel *mat_main_Label;
@property (nonatomic, retain) UILabel *mat_other_Label;

@property (nonatomic, assign) CGFloat mat_height;
@property (nonatomic, assign) CGFloat bur_height;


@property (nonatomic, retain) NSMutableArray *lastArr;


@property (nonatomic, retain) UILabel *stepLabel;
@property (nonatomic, retain) UIButton *addButton;

@property (nonatomic, assign) BOOL isSelected;

@end

@implementation Cook_Thir_ViewController

-(void)viewWillAppear:(BOOL)animated
{
    [self.cook_Thir_View.tableView reloadData];
    self.tabBarController.tabBar.hidden = YES;
}


-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [[DataBase shareDataBaseHandle] openDB];
    self.isSelected = NO;
  
    
    self.lastArr = [[NSMutableArray alloc]init];
    
    self.cook_Thir_View = [[Cook_Thir_View alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:self.cook_Thir_View];
    
    for (NSMutableDictionary *dic in self.stepArr) {
        
        CookModel *cook = [[CookModel alloc]init];
        [cook setValuesForKeysWithDictionary:dic];
        cook.height = [HeightForText heightForText:cook.step width:SCREEN_WIDTH-40 font:16];
        [self.lastArr addObject:cook];
        
    }
    
    self.cook_Thir_View.titleLabel.text = self.model.title;
    self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 200)];
    [self.img sd_setImageWithURL:[NSURL URLWithString:self.pic]placeholderImage:[UIImage imageNamed:@"default"]];
    [self.cook_Thir_View.backgroundView addSubview:self.img];
    
    [self tagBg];
    [self Button];
    
    // Do any additional setup after loading the view.
}


-(void)tagBg
{
        
    self.cook_Thir_View.tableView.delegate = self;
    self.cook_Thir_View.tableView.dataSource = self;

    self.tagBgLabel = [[UILabel alloc]init];
    self.tagBgLabel.font = [UIFont systemFontOfSize:16];
    self.tagBgLabel.numberOfLines = 0;
    self.tagBgLabel.text = self.tag;
    
    
//    自适应高
    self.height = [HeightForText heightForText:self.tag width:SCREEN_WIDTH-40 font:16];
    self.tagBgLabel.frame = CGRectMake(20, 210 + 80, SCREEN_WIDTH - 40, self.height);
    [self.cook_Thir_View.backgroundView addSubview:self.tagBgLabel];
    self.materials = [[UILabel alloc]initWithFrame:CGRectMake(20, self.height + 210 + 90, 200, 30)];
    self.materials.text = @"用料";
    self.materials.font = [UIFont systemFontOfSize:18];
    [self.cook_Thir_View.backgroundView addSubview:self.materials];
    self.materials.textColor = [UIColor colorWithRed:192./255. green:130./255. blue:80./255. alpha:1.];
    
    self.mat_main = [[UILabel alloc]initWithFrame:CGRectMake(40, self.height + 210 + 90 + 30, 200, 30)];
    self.mat_main.text = @"主料";
    self.mat_main.textColor = [UIColor lightGrayColor];
    self.mat_main.font = [UIFont systemFontOfSize:16];
    [self.cook_Thir_View.backgroundView addSubview:self.mat_main];
    
    self.mat_main_Label = [[UILabel alloc]init];
    self.mat_main_Label.text = self.ing;
    self.mat_main_Label.font = [UIFont systemFontOfSize:16];
    self.mat_main_Label.numberOfLines = 0;
    [self.cook_Thir_View.backgroundView addSubview:self.mat_main_Label];

  
//    自适应高度
    self.mat_height = [HeightForText heightForText:self.ing width:SCREEN_WIDTH-40 font:16];
    self.mat_main_Label.frame = CGRectMake(40, self.height + 360, 300, self.mat_height);
    self.mat_other = [[UILabel alloc]initWithFrame:CGRectMake(40, self.height + 210 + 90 + 30 + 30 + self.mat_height, 200, 30)];
    self.mat_other.text = @"配料";
    self.mat_other.textColor = [UIColor lightGrayColor];
    self.mat_other.font = [UIFont systemFontOfSize:16];
    [self.cook_Thir_View.backgroundView addSubview:self.mat_other];

    
    self.mat_other_Label = [[UILabel alloc]init];
    self.mat_other_Label.text = self.bur;
    self.mat_other_Label.font = [UIFont systemFontOfSize:16];
    self.mat_other_Label.numberOfLines = 0;
    [self.cook_Thir_View.backgroundView addSubview:self.mat_other_Label];

    
//    自适应高度
    self.bur_height = [HeightForText heightForText:self.bur width:SCREEN_WIDTH - 40 font:16];
    self.mat_other_Label.frame = CGRectMake(40, self.height + 210 + 90 + 90 + self.mat_height,SCREEN_WIDTH - 60, self.bur_height);
    
    
    self.stepLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, self.height + self.mat_height + self.bur_height + 400, 200, 30)];
    self.stepLabel.text = @"做法";
    self.stepLabel.textColor = [UIColor colorWithRed:192./255. green:130./255. blue:80./255. alpha:1.];
    self.stepLabel.font = [UIFont systemFontOfSize:18];
    [self.cook_Thir_View.backgroundView addSubview:self.stepLabel];

    
    
    self.cook_Thir_View.backgroundView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height + 410 + self.mat_height + self.bur_height);
    self.cook_Thir_View.tableView.tableHeaderView = self.cook_Thir_View.backgroundView;
    
}



#pragma mark- UItableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lastArr.count;
//    return 10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CookModel * mo = [self.lastArr objectAtIndex:indexPath.row];
    return 240 + mo.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    Cook_Thir_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[Cook_Thir_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    CookModel *model = [self.lastArr objectAtIndex:indexPath.row];
//    [cell get:model];
    [cell getModel:model];
   

    return cell;
}




#pragma mark- UIBarButtonItem
-(void)Button
{

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"shoucang"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(addClick)];
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 30, 20);
    [leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"backk.png"] forState:UIControlStateNormal];
    UIBarButtonItem * custom = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = custom;
  
        self.navigationItem.rightBarButtonItem = addButton;

    
}



#pragma mark-  点击收藏
-(void)addClick
{
    
    if ([[DataBase shareDataBaseHandle] selectCook].count == 0) {
    
        _isSelected = YES;
    }
   
    
    for (CookModel *c in [[DataBase shareDataBaseHandle] selectCook]) {

        if (![c.title isEqualToString:self.model.title]){
            _isSelected = YES;
            
        }else {
 
            _isSelected = NO;
            break;
        }
    }
    
    if (_isSelected == YES) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"是否收藏菜谱" message:nil delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
        [alert show];

    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您已经收藏过了" message:nil delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];

    }

}




#pragma mark-  UIAlaterView
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        
        
    }  else {
        
        if ([[DataBase shareDataBaseHandle] selectCook].count == 0) {
            
            [[DataBase shareDataBaseHandle] creatTable];
            
            
            [[DataBase shareDataBaseHandle] insertCookInfo:self.model];
       
        }
        
        NSInteger count = 0;
        for (CookModel *coco in [[DataBase shareDataBaseHandle] selectCook]) {
            
            if (![coco.title isEqualToString:self.model.title]) {
                
                count ++;
                
                if ([[DataBase shareDataBaseHandle] selectCook].count == count) {
                    
                    [[DataBase shareDataBaseHandle] insertCookInfo:self.model];
                }
            }
        }
        
    }
}




-(void)shareClick
{
//    //注意：分享到微信好友、微信朋友圈、微信收藏、QQ空间、QQ好友、来往好友、来往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要参考各自的集成方法
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"5538d80067e58e136e001c73"
//                                      shareText:@"你要分享的文字"
//                                     shareImage:nil
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
//                                       delegate:self];
}


-(void)leftClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

















/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
